﻿using System;
using System.Collections.Generic;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using TreeViewForXamarin.Adapters;
using TreeViewForXamarin.Models;

namespace TreeViewForXamarin
{
    [Activity(Label = "RecyclerViewer", MainLauncher = true, Icon = "@drawable/icon",Theme = "@android:style/Theme.Material.Light.DarkActionBar")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            List<TreeNode> treeNodes = new List<TreeNode>();

            treeNodes.Add(new TreeNode("节点1", 1, null));
            List<TreeNode> treeNodes1 = new List<TreeNode>();
            treeNodes1.Add(new TreeNode("节点11", 11, treeNodes[0]));
            treeNodes1.Add(new TreeNode("节点12", 12, treeNodes[0]));
            treeNodes1.Add(new TreeNode("节点13", 13, treeNodes[0]));
            List<TreeNode> treeNodes11 = new List<TreeNode>();
            treeNodes11.Add(new TreeNode("节点111", 111, treeNodes1[0]));
            treeNodes11.Add(new TreeNode("节点112", 112, treeNodes1[0]));
            treeNodes1[0].SetChildTreeNodes(treeNodes11);
            treeNodes[0].SetChildTreeNodes(treeNodes1);

            treeNodes.Add(new TreeNode("节点2", 2, null));
            List<TreeNode> treeNodes2 = new List<TreeNode>();
            treeNodes2.Add(new TreeNode("节点21", 21, treeNodes[1]));
            treeNodes2.Add(new TreeNode("节点22", 22, treeNodes[1]));
            treeNodes2.Add(new TreeNode("节点23", 23, treeNodes[1]));
            treeNodes[1].SetChildTreeNodes(treeNodes2);

            treeNodes.Add(new TreeNode("节点3", 3, null));
            List<TreeNode> treeNodes3 = new List<TreeNode>();
            treeNodes3.Add(new TreeNode("节点31", 31, treeNodes[2]));
            treeNodes3.Add(new TreeNode("节点32", 32, treeNodes[2]));
            treeNodes[2].SetChildTreeNodes(treeNodes3);

            treeNodes.Add(new TreeNode("节点4", 4, null));
            treeNodes.AddRange(treeNodes1);
            treeNodes.AddRange(treeNodes11);
            treeNodes.AddRange(treeNodes2);
            treeNodes.AddRange(treeNodes3);

            //创建适配器
            RecyclerTreeListAdapter adapter = new RecyclerTreeListAdapter(this, treeNodes);
            adapter.SelecteItemChanged += Adapter_SelecteItemChanged; 
            //获取RecyclerView控件
            RecyclerView recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
            //设置LayoutManager（度量和定位中的项视图 RecyclerView ，并管理视图回收策略）
            recyclerView.SetLayoutManager(new LinearLayoutManager(this));
            recyclerView.SetAdapter(adapter);
        }

        #region # 树适配器选中项变更事件
        /// <summary>
        /// 树适配器选中项变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Adapter_SelecteItemChanged(object sender, TreeNode e)
        {
            TextView tv_SelectedTreeNode = FindViewById<TextView>(Resource.Id.tv_SelectedTreeNode);
            tv_SelectedTreeNode.Text = e.TreeNodeName;

            Toast.MakeText(this, "当前选中节点：" + e.TreeNodeName, ToastLength.Short).Show();
        } 
        #endregion

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
	}
}
